package com.ss.android.newmedia.webview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.bytedance.common.b.MixedContentCookieCompat;
import com.bytedance.common.b.UserAgentCompat;
import com.bytedance.common.b.WebViewPauseResumeCompat;
import com.bytedance.common.utility.Logger;

import java.lang.ref.WeakReference;
import java.util.LinkedHashSet;
import java.util.Set;

public final class SSWebSettings {
    public boolean hardwareAccelerate;
    private WeakReference mContextRef;
    private boolean mJavaScriptEnabled;
    private boolean mSupportZoom;
    private boolean mUseWideViewPort;
    private boolean mDomStorageEnabled;
    private boolean mAllowFileAccess;
    private boolean mAllowNetworkImage;
    private static Set i;

    static {
        LinkedHashSet v0 = new LinkedHashSet();
        i = ((Set) v0);
        ((Set) v0).add("device_id");
        i.add("ac");
        i.add("channel");
        i.add("aid");
        i.add("device_platform");
        i.add("device_type");
        i.add("os_api");
        i.add("update_version_code");
    }

    private SSWebSettings(Context context) {
        this.mJavaScriptEnabled = true;
        this.mSupportZoom = true;
        this.mUseWideViewPort = true;
        this.mDomStorageEnabled = true;
        this.mAllowFileAccess = true;
        this.mAllowNetworkImage = true;
        this.hardwareAccelerate = true;
        this.mContextRef = new WeakReference(context);
    }

    public static SSWebSettings createWebSettings(Context context) {
        return new SSWebSettings(context);
    }

    @SuppressLint(value = {"SetJavaScriptEnabled"})
    public final void setJavaScriptEnabled(WebView webView) {
        if (webView != null && this.mContextRef.get() != null) {
            WebSettings webViewSettings = webView.getSettings();
            if (webViewSettings == null) {
                return;
            }

            try {
                webViewSettings.setJavaScriptEnabled(this.mJavaScriptEnabled);
            } catch (Exception exception) {
                Logger.w("SSWebSettings", "setJavaScriptEnabled failed");
            }

            try {
                if (this.mSupportZoom) {
                    webViewSettings.setSupportZoom(true);
                    webViewSettings.setBuiltInZoomControls(true);
                    WebViewPauseResumeCompat.setDisplayZoomControls(webViewSettings);
                } else {
                    webViewSettings.setSupportZoom(false);
                }
            } catch (Throwable throwable) {
            }

            webViewSettings.setLoadWithOverviewMode(true);
            webViewSettings.setUseWideViewPort(this.mUseWideViewPort);
            webViewSettings.setDomStorageEnabled(this.mDomStorageEnabled);
            webViewSettings.setAllowFileAccess(this.mAllowFileAccess);
            webViewSettings.setBlockNetworkImage(!this.mAllowNetworkImage);
            if (!this.hardwareAccelerate) {
                try {
                    ViewCompat.setLayerType(webView, ViewCompat.LAYER_TYPE_SOFTWARE, null);
                } catch (Throwable throwable) {
                }
            }

            UserAgentCompat.setMediaPlaybackRequiresUserGesture(webView.getSettings());
            MixedContentCookieCompat.setMixedContentMode(webView.getSettings());
            MixedContentCookieCompat.setAcceptThirdPartyCookies(webView);
        }
    }

    public static String buildAdJs(String jsActLogUrl, long ad_id) {
        return (android.support.design.a.isTextEmpty(jsActLogUrl)) || ad_id <= 0 || !jsActLogUrl.contains("{{ad_id}}")
                ? null
                : "javascript:(function () {" +
                "    var JS_ACTLOG_URL = \'" + jsActLogUrl.replace("{{ad_id}}", String.valueOf(ad_id)) + "\';" +
                "    var head = document.getElementsByTagName(\'head\')[0];" +
                "    var script = document.createElement(\'script\');" +
                "    script.type = \'text/javascript\';" +
                "    script.src = JS_ACTLOG_URL;" +
                "    head.appendChild(script);})();";
    }

}

